home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / comm / net / spakparnet_0_5.lha / srv / srv.c < prev    next >
C/C++ Source or Header  |  1992-11-09  |  6KB  |  229 lines

  1. /********************************************************************
  2.  ** NETWORK FILE SYSTEM SERVICE v0.5
  3.  **
  4.  ** (c) Spak, Darrell Tam, c9107253@ee.newcastle.edu.au (1994)
  5.  ** phone (Australia) 049-829-710
  6.  **                    49-829-710
  7.  ** (c) SST Jan-Feb-Mar 1994
  8.  **
  9.  ** A NETWORK FILE SYSTEM SERVICE WHICH REGISTERS WITH ParServer or
  10.  ** compatible servers
  11.  ** 
  12.  ** the format of the messages sent/received are as shown in parstuff.h 
  13.  ** which is a "generic" format
  14.  **
  15.  ** features:
  16.  ** - all operations are totally asynchronous
  17.  ** - reading is double buffered for best performance (hence the code!)
  18.  **
  19.  ** bugs:
  20.  ** - writing is not double buffered
  21.  **
  22.  **
  23.  ** TABS to 4
  24.  ********************************************************************/
  25.  
  26. #include "/snd/everything.h"
  27. #include <st/st_proto.h>
  28. #include <string.h>
  29. #include "nfsinternal.h"
  30.  
  31. #include "nfsglobs_protos.h"
  32. #include "nfsopen_protos.h"
  33. #include "nfsclose_protos.h"
  34. #include "nfsread_protos.h"
  35. #include "nfswrite_protos.h"
  36. #include "nfsseek_protos.h"
  37. #include "nfslock_protos.h"
  38. #include "nfsfreelock_protos.h"
  39. #include "nfswaitchar_protos.h"
  40. #include "nfsexamine_protos.h"
  41.  
  42. short    netport = SERV_FILE;                /* network "port" number */
  43. char    *netregname = "ParRegister";        /* server to "register" with */
  44.  
  45.  
  46. /********************************************************************/
  47.     void CleanUp(void)
  48. /********************************************************************/
  49. {
  50. /** REMOVE US AS A SERVER FROM THE THING */
  51.     if(registered) {
  52.     struct RegisterMsg rm;
  53.     struct Message *maj;
  54.  
  55.         INIT_REGISTERMSG(&rm, ourport, RM_REMOVE_SERVER, netport, NULL, NULL); 
  56.         for(PutMsg(parport, &rm); (WaitPort(ourport), (maj = GetMsg(ourport)) != &rm);)
  57.                     ReplyMsg(maj); /* important to return all messages but ours */
  58.     }
  59.     if(ourport) DeletePort(ourport);
  60.     if(dosport) DeletePort(dosport);
  61.     if(GfxBase) CloseLibrary((struct Library *)GfxBase);
  62.     if(IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
  63.  
  64. }
  65.  
  66.  
  67. /********************************************************************/
  68.     short InitStuff(void)
  69. /********************************************************************/
  70. {
  71.     out = Output();
  72.     if(!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0))
  73.     || !(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0)) )
  74.         return(0);
  75.     if(!(ourport = CreatePort(0,0)) ||            /* port to send packs to */
  76.        !(dosport = CreatePort(0,0))) {            /* comms with dos */
  77.         fpf(out, "No ports\n");
  78.         return(0);
  79.     }
  80.     if(!(parport = FindPort(netregname)) ) {
  81.         fpf(out, "Couldn't find '%s'\n", netregname);
  82.         return(0);
  83.     }
  84.  
  85. /** REGISTER WITH THE PARALLEL THING */
  86. {
  87. struct RegisterMsg rm;
  88.     INIT_REGISTERMSG(&rm, ourport, RM_ADD_SERVER, netport, ourport, NULL); 
  89.     PutMsg(parport, &rm); WaitPort(ourport); GetMsg(ourport);
  90.  
  91.     if(rm.smsg.result != (long)ourport) {
  92.         fpf(out, "Netport (%ld) is already in use ('%s')\n",
  93.             netport,
  94.             ((struct MsgPort *)rm.smsg.result)->mp_Node.ln_Name);
  95.         return(0);
  96.     }
  97.     registered = 1;
  98.     fpf(out, "Registered ok (netport=%ld)\n", netport);
  99. }
  100.     return(1);
  101. }
  102.  
  103.  
  104. /********************************************************************/
  105.     void DoMajic(void)
  106. /********************************************************************/
  107. {
  108.  
  109. unsigned long mask, waitmask, pmask, dmask;
  110. register struct PktMsgRecv *pmr;
  111.  
  112.     waitmask =    (pmask = 1<<ourport->mp_SigBit) |
  113.                 (dmask = 1<<dosport->mp_SigBit) |
  114.                 SIGBREAKF_CTRL_C;
  115.  
  116.     fpf(out, "Waiting for requests\n");
  117.     while(/* packets outstanding && */ 
  118.         stillgoing && !((mask = Wait(waitmask)) & SIGBREAKF_CTRL_C)) {
  119.  
  120.     /** THIS PORT IS ONLY MEANT FOR DIRECT-COMMS WITH THE SERVER */
  121.         if(mask & pmask) while(pmr = (struct PktMsgRecv *)GetMsg(ourport) ) {
  122.  
  123. #ifdef DEBUG
  124.             fpf(out,"nfs: recv (msg=%lx, type=%ld, alloclen=%ld)\n",
  125.                 pmr, pmr->pm.smsg.type, pmr->pm.smsg.alloclen);
  126. #endif
  127.             switch(pmr->pm.smsg.type) {
  128.                 case TYPE_RECEIVED: {
  129.                 struct NFSDummy *nfs_req = (void *)pmr->req_body;
  130.  
  131.                 /* the action table with action numbers shown */
  132.                 static void *(* action[])(struct PktMsgRecv *) = {
  133.                     srv_examinelock,        /* 0 */
  134.                     srv_openfile,            /* 1 */
  135.                     srv_closefile,            /* 2 */
  136.                     srv_read,                /* 3 */
  137.                     srv_write,                /* 4 */
  138.                     srv_seek,                /* 5 */
  139.                     srv_lock,                /* 6 */
  140.                     srv_freelock,            /* 7 */
  141.                     srv_waitchar,            /* 8 */
  142.                     srv_copylock,            /* 9 */
  143.                     srv_examinenext,        /* 10 */
  144.                 };
  145.  
  146.                 register void *err_mem;
  147.                 register long request;
  148. #ifdef DEBUG
  149.                     fpf(out, "nfs: action=%ld\n", nfs_req->request);
  150. #endif
  151.  
  152.                 /** CHANGE THE NET-SERVER-MESSAGE INTO ONE FOR US */
  153.                     INIT_MSG(&pmr->pm.smsg.msg,  ourport, sizeof(*pmr));
  154.  
  155.                     if((request = nfs_req->request) < (sizeof(action)/sizeof(void *)))
  156.                         err_mem = action[request](pmr);
  157.                     else
  158.                         err_mem = &nfs_res_fail;
  159.  
  160.                 /** IF THERE WAS A BAD ERROR RETURN THE PACKET IMMEDIATELY */
  161.                     if(err_mem) {
  162.                         if(nfs_req->sr.return_len) {
  163.                             pmr->user.user1 = NULL;            /* free on return */
  164.                             INTERNAL_PREP_PKTMSG_DMA(&pmr->pm,
  165.                                 EMEM_RETURNMSG,
  166.                                 SERV_RETURN,
  167.                                 err_mem, nfs_req->sr.return_len);
  168.                             PUT_STDMSG(&pmr->pm.smsg,
  169.                                         pmr->dmaport, TYPE_INTERNAL_SEND);
  170.                         }
  171.                         else FREE_PKTMSGRECV(pmr);
  172.                     }
  173.                 }
  174.                 break;
  175.  
  176.             /** DMA PACKET RETURNED (FREE MEM IF NO ROUTINE) */
  177.                 case  TYPE_RETURN: {
  178.                     /* for the moment ignore send-errors ... */
  179.                     if((long)pmr->user.user1)
  180.                             pmr->user.user1(pmr);
  181.                                 else
  182.                             FREE_PKTMSGRECV(pmr);        /* free the message */
  183.                 }
  184.                 break;
  185.  
  186.             /** SERVER TRYING TO KILL US ? */
  187.                 case TYPE_QUIT: {
  188.                     stillgoing = 0;
  189.                     ReplyMsg(pmr);
  190.                 }
  191.                 break;
  192.  
  193.             /** DUNNO WHAT THIS IS BUT REPLY TO IT ANYWAY */
  194.                 default:
  195.                     ReplyMsg(pmr);
  196.             }
  197.         }
  198.  
  199.     /** GET RETURN PACKET FROM DOS (call the function contained in the dosmsg) */
  200.         if(mask & dmask) {
  201.         struct DosMsg *dm;
  202.             while(dm = (struct DosMsg *)GetMsg(dosport))
  203.                 if(dm->code) dm->code(dm);
  204.         }
  205.     }
  206. }
  207.  
  208.  
  209.  
  210.  
  211. /********************************************************************/
  212.     int main(int argc, char *argv[])
  213. /********************************************************************/
  214. {
  215. struct Process *pr = (void *)FindTask(NULL);
  216. void *olddoswindow;
  217. /*    if(argc >= 1) {
  218.         netregname = argv[1];                    /* eg "ParServer" */
  219.         if(argc >= 2) 
  220.             netport = atoi(argv[2]);
  221.     } */
  222.     olddoswindow = pr->pr_WindowPtr;
  223.     pr->pr_WindowPtr = (void *)-1;                    /* turn off dos-request windows */
  224.     if(InitStuff()) DoMajic();
  225.     CleanUp();
  226.     pr->pr_WindowPtr = olddoswindow;
  227. }
  228.  
  229.